from django.shortcuts import render
from django.http import JsonResponse
<<<<<<< HEAD
from django.core import serializers
from .models import Book

import json
# Create your views here.


# @require_http_methods(["GET"])
=======
from django.views.decorators.http import require_http_methods
from django.core import serializers


import requests
import json

from .models import Book, WellCondition
# Create your views here.


@require_http_methods(["GET"])
>>>>>>> 2a64c6a493d750af12224840a8b05b087062d6b2
def add_book(request):
    response = {}
    try:
        book = Book(book_name=request.GET.get('book_name'))
        book.save()
        response['msg'] = 'success'
        response['error_num'] = 0
    except Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1

    return JsonResponse(response)


<<<<<<< HEAD
# @require_http_methods(["GET"])
def show_books(request):
    response = {}
    try:
        books = Book.objects.filter()
        response['list'] = json.loads(serializers.serialize("json", books))
        response['msg'] = 'success'
        response['error_num'] = 0
    except Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1

    return JsonResponse(response)
=======
@require_http_methods(["GET"])
def show_books(request):
    response_data = {}
    try:
        books = Book.objects.filter()
        response_data['list'] = json.loads(serializers.serialize("json",books))
        response_data['msg'] = 'success'
        response_data['error_num'] = 0
    except Exception as e:
        response_data['msg'] = str(e)
        response_data['error_nmu'] = 1

    return JsonResponse(response_data)


@require_http_methods(["GET"])
def add_well_condition(request):
    response_data = {}
    try:
        well_condition = WellCondition(id=request.GET.get('id'),well_type=request.GET.get('well_type'),quantity =request.GET.get('quantity'))
        well_condition.save()
        response_data ['msg'] = 'success'
        response_data ['error_num'] = 0
    except Exception as e:
        response_data ['msg'] = str(e)
        response_data ['error_num'] = 1

    return JsonResponse(response_data)


@require_http_methods(["GET"])
def show_well_conditions(request):
    response_data = {}
    try:
        well_conditions = WellCondition.objects.filter()
        response_data['list'] = json.loads(serializers.serialize("json",well_conditions))
        response_data['msg'] = 'success'
        response_data['error_num'] = 0
    except Exception as e:
        response_data['msg'] = str(e)
        response_data['error_nmu'] = 1

    return JsonResponse(response_data)
>>>>>>> 2a64c6a493d750af12224840a8b05b087062d6b2
